package com.example.demo.mapper;

import com.example.demo.entity.RelSendRecords;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author
 * @Description
 */
@Mapper
@Repository
public interface RelSendRecordsMapper {

    @Insert(
            "<script>" +
                    "INSERT INTO `rel_send_records`(`send_id`,`invoice_id`,`invoice_code`,`invoice_number`,`failed_reason`)" +
                    "VALUES " +
                    "<foreach collection='search' item='item' index='index' separator=',' open=' ' close=';'>" +
                    "(#{item.sendId},#{item.invoiceId},#{item.invoiceCode},#{item.invoiceNumber},#{item.failedReason})" +
                    "</foreach>" +
                    "</script>")
    @Options(useGeneratedKeys = true, keyProperty = "relId", keyColumn = "rel_id")
    Long insertRelSendRecordsList(@Param("search") List<RelSendRecords> relSendRecordsList);

    @Select("SELECT `rel_id`,`send_id`,`invoice_id`,`invoice_code`,`invoice_number`,`failed_reason` " +
            "FROM `rel_send_records` " +
            "WHERE `send_id` = #{sendId}")
    @Results(id = "relSendRecords", value = {
            @Result(id = true, property = "relId",column = "rel_id"),
            @Result(property = "sendId", column = "send_id"),
            @Result(property = "invoiceId", column = "invoice_id"),
            @Result(property = "invoiceNumber", column = "invoice_number"),
            @Result(property = "failedReason", column = "failed_reason")
    })
    List<RelSendRecords> findRelSendRecordsBySendId(@Param("sendId") Long sendId);

    @Select("SELECT `invoice_id` FROM `rel_send_records` WHERE `send_id` = #{sendId}")
    List<String> findInvoiceIdListBySendId(Long sendId);
}
